const path = require('path')

const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals')

const isDev = process.argv.indexOf('--develop') >= 0
const isWatch = process.argv.indexOf('--watch') >= 0
const demoSrc = path.resolve(__dirname, './demo')
const demoDist = path.resolve(__dirname, '../miniprogram_dev')
const src = path.resolve(__dirname, '../src')
const dev = path.join(demoDist, 'components')
const dist = path.resolve(__dirname, '../miniprogram_dist')

module.exports = {
  entry: [
    'index',
    'behaviors',
    'utils',
    'avatar/index',
    'actions/index',
    'backdrop/index',
    'button/index',
    'button-group/index',
    'card/index',
    'cell/index',
    'checkable/index',
    'checkbox/index',
    'checkbox-group/index',
    'dialog/index',
    'fab/index',
    'icon/index',
    'input/index',
    'label/index',
    'list/index',
    'picker/index',
    'popup/index',
    'radio/index',
    'radio-group/index',
    'slider/index',
    'spacer/index',
    'static/index',
    'switch/index',
    'text/index',
    'textarea/index',
    'uploader/index',
    'page/index',
    'form-validator/index',
  ],

  isDev,
  isWatch,
  srcPath: src, // 源目录
  distPath: isDev ? dev : dist, // 目标目录

  demoSrc, // demo 源目录
  demoDist, // demo 目标目录

  wxss: {
    less: true, // 使用 less 来编写 wxss
    sourcemap: true, // 生成 less sourcemap
  },

  js: {
    webpack: true, // 使用 webpack 来构建 js
  },

  webpack: {
    mode: 'production',
    output: {
      filename: '[name].js',
      libraryTarget: 'commonjs2',
    },
    target: 'node',
    externals: ['../behaviors', '../utils', nodeExternals()], // 忽略 node_modules
    module: {
      rules: [
        {
          test: /\.js$/i,
          use: [
            {
              loader: 'thread-loader',
            },
            {
              loader: 'babel-loader',
              options: {
                cacheDirectory: true,
              },
            },
            {
              loader: 'eslint-loader',
            },
          ],
          exclude: /node_modules/,
        },
        {
          test: /\.ts$/,
          exclude: /node_modules/,
          use: [
            {
              loader: 'thread-loader',
            },
            {
              loader: 'babel-loader',
              options: {
                cacheDirectory: true,
              },
            },
            {
              loader: 'ts-loader',
              options: {
                appendTsSuffixTo: [/\.vue$/],
                happyPackMode: true,
              },
            },
            {
              loader: 'eslint-loader',
            },
          ],
        },
      ],
    },
    resolve: {
      modules: [src, 'node_modules'],
      extensions: ['.js', '.json'],
    },
    plugins: [
      new webpack.DefinePlugin({}),
      new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
    ],
    optimization: {
      minimize: false,
    },
    devtool: 'source-map', // 生成 js sourcemap
    performance: {
      hints: 'warning',
      assetFilter: (assetFilename) => assetFilename.endsWith('.js'),
    },
  },

  copy: ['./_styles'], // 将会复制到目标目录
}
